#!/usr/bin/env bash

TAG=$1
REPO=$2
PUSH=$3

. $(dirname $0)/util
set -eu -o pipefail

: ${PLATFORMS=linux/amd64}
: ${TARGET=}

versionTag=$(git describe --always --tags --match "v[0-9]*")

if [[ ! "$versionTag" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  versionTag=""
fi

usage() {
  echo "usage: $0 <tag> <repo> [push]"
  exit 1
}

if [ -z "$TAG" ] || [ -z "$REPO" ]; then
  usage
fi

localmode=""
if [[ "$TAG" == "local" ]]; then
  localmode="1"
  if [ "$PUSH" = "push" ]; then
    echo >&2 "local images cannot be pushed"
    exit 1
  fi
fi

outputFlag="--output=type=image,push=false"
if [ "$PUSH" = "push" ]; then
  outputFlag="--output=type=image,push=true"
fi
if [ -n "$localmode" ]; then
  outputFlag="--output=type=docker"
fi

targetFlag=""
if [ -n "$TARGET" ]; then
  targetFlag="--target=$TARGET"
fi

tagNames="$REPO:$TAG"
if [ -n "$TARGET" ]; then
  tagNames="$tagNames-$TARGET"
fi

if [[ "$versionTag" == "$TAG" ]]; then
  if [ -n "$TARGET" ]; then
    tagNames="$tagNames $REPO:$TARGET"
  else
    tagNames="$tagNames $REPO:latest"
  fi
fi

importCacheFlags=""
for tagName in $tagNames; do
  importCacheFlags="$importCacheFlags--cache-from=type=registry,ref=$tagName "
done
if [[ -n "$cacheRefFrom" ]] && [[ "$cacheType" = "local" ]]; then
  for ref in $cacheRefFrom; do
    importCacheFlags="$importCacheFlags--cache-from=type=local,src=$ref "
  done
fi
if [ -n "$localmode" ]; then
  importCacheFlags=""
fi

exportCacheFlags=""
if [[ -n "$cacheRefTo" ]] && [[ "$cacheType" = "local" ]]; then
  exportCacheFlags="--cache-to=type=local,dest=$cacheRefTo "
elif [ "$PUSH" = "push" ]; then
  exportCacheFlags="$exportCacheFlags--cache-to=type=inline "
fi

tagFlags=""
for tagName in $tagNames; do
  tagFlags="$tagFlags--tag=$tagName "
done

buildxCmd build $targetFlag $importCacheFlags $exportCacheFlags $tagFlags $outputFlag \
  --platform "$PLATFORMS" \
  $currentcontext
